Conditions | 11 |
Total Lines | 19 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Complex classes like image-preview.js ➔ displayUploadedImage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | (function ( $ ) { |
||
24 | function displayUploadedImage(input) { |
||
25 | if (input.files && input.files[0]) { |
||
26 | var reader = new FileReader(); |
||
|
|||
27 | |||
28 | reader.onload = function (e) { |
||
29 | var image = $(input).parent().siblings('.image'); |
||
30 | |||
31 | if (image.length > 0) { |
||
32 | image.attr('src', e.target.result); |
||
33 | } else { |
||
34 | var img = $('<img class="ui small bordered image"/>'); |
||
35 | img.attr('src', e.target.result); |
||
36 | $(input).parent().before(img); |
||
37 | } |
||
38 | }; |
||
39 | |||
40 | reader.readAsDataURL(input.files[0]); |
||
41 | } |
||
42 | } |
||
43 | })( jQuery ); |
||
50 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.